import React, {Component} from 'react';
import {Modal, Button} from 'antd';
import {orderList} from "../value";
import app from '../AppMethod';
import {config} from "../config";

//登录界面
class Login extends Component {
    constructor(props) {
        super(props);
        this.state = {
            stockRoom: [],
            editVisible: false,
            confirmLoading: false,
            stockRoom_id: ''
        }
    };

    render() {
        return (
            <div>
                {
                    this.state.stockRoom.map((item, index) => {
                        return (
                            <div key={index} className="stockRoom"
                                 onClick={() => this.chooseStockRoom(item.stock_id)}>{item.stock_name}</div>
                        )
                    })
                }
                <Modal
                    maskClosable={true}
                    visible={this.state.editVisible}
                    confirmLoading={this.state.confirmLoading}
                    wrapClassName="vertical-center-modal"
                    title="选择展示端"
                    width="400px"
                    onCancel={() => this.setModalVisible(false)}
                    footer={[
                        <Button key="cancel" onClick={() => this.setModalVisible(false)}>取消</Button>
                    ]}
                >
                    <Button type="primary" style={{width: '100%', height: '40px'}} onClick={() => this.handleShow('1')}>入库展示端</Button>
                    <Button type="primary" style={{width: '100%', height: '40px', marginTop: '20px'}}
                            onClick={() => this.handleShow('2')}>出库展示端</Button>
                    <Button type="primary" style={{width: '100%', height: '40px', marginTop: '20px'}}
                            onClick={() => this.handleShow('3')}>配送展示端</Button>
                </Modal>
            </div>
        )
    };

    componentDidMount() {
        this.handleSearchStock();
    };

    componentWillUnmount() {
        this.setState = (count, callback) => {
            return;
        }
    };

    /**
     * 处理选择仓库事件
     * @param stockRoom_id
     */
    chooseStockRoom(stockRoom_id) {
        //this.props.history.push(`/delivery/${stockRoom_id}`);
        this.setState({stockRoom_id});
        this.setModalVisible(true);
    };

    /**
     * 处理选择不同展示端事件
     * @param type
     */
    handleShow(type) {
        let path = '';
        if (type == 1) {
            path = 'storageManage';
        } else if (type == 2) {
            path = 'outPutManage';
        } else if (type == 3) {
            path = 'delivery';
        }
        this.props.history.push(`${config.path}/${path}/${this.state.stockRoom_id}`);
    }

    /**
     * 获取仓库列表
     */
    handleSearchStock() {
        app.post(orderList.SEARCH_STOCK, {}).then((result) => {
            if (result.code === 1) {
                let data = result.data.dataSet;
                this.setState({stockRoom: data});
            } else {
                app.alert(result.msg, 'error', 3);
                this.setState({stockRoom: []});
            }
        })
    };

    /**
     * 显示和隐藏弹框
     * @param visible
     */
    setModalVisible(visible) {
        this.setState({editVisible: visible});
    }
}

export default Login;